package pl.mchaniewski.ryneczek.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.mchaniewski.ryneczek.dao.OrderDao;
import pl.mchaniewski.ryneczek.pojo.Order;

@Repository("orderDao")
@Transactional(readOnly = true)
public class HibernateOrderDao extends HibernateGenericDao<Order, Integer>
        implements OrderDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findByPortfolioIdStatusAndTransactionType(
            Integer portfolioId, String status, String transactionType) {
        String queryString = "from Order o where o.portfolio.portfolioid=:portfolioId and o.status=:activeStatus and o.status=:activeStatus and o.transactionType=:transType";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("portfolioId", portfolioId);
        query.setParameter("activeStatus", status);
        query.setParameter("transType", transactionType);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findByPortfolioIdAndStatus(Integer portfolioId,
            String status) {
        String queryString = "from Order o where o.portfolio.portfolioid=:portfolioId and o.status=:activeStatus order by o.submissionDate desc";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("portfolioId", portfolioId);
        query.setParameter("activeStatus", status);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findByStatusPortfolioIdAndCompanyId(Integer portfolioId,
            Integer companyId, String status) {
        String queryString = "from Order o where o.portfolio.portfolioid=:portfolioId and o.company.companyid=:companyId and o.status=:activeStatus";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("portfolioId", portfolioId);
        query.setParameter("companyId", companyId);
        query.setParameter("activeStatus", status);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findAllWithStatusBeforeDate(String status, Date date,
            boolean companyWasInLastUpdate) {
        String queryString = "from Order o where o.status=:status and o.submissionDate<=:beforeDate and o.company.wasInLastUpdate=:companyWasInLastUpdate";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("status", status);
        query.setParameter("beforeDate", date);
        query.setParameter("companyWasInLastUpdate",
                Boolean.valueOf(companyWasInLastUpdate));

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findAllButAcceptedByPortfolioId(Integer portfolioId) {
        String queryString = "from Order o where o.portfolio.portfolioid=:portfolioId and o.status<>:activeStatus order by o.expiryDate desc";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("portfolioId", portfolioId);
        query.setParameter("activeStatus", Order.STATUS_ACCEPTED);

        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Order> findAllButAcceptedByPortfolioIdAndCompanyId(
            Integer portfolioId, Integer companyId) {
        String queryString = "from Order o where o.portfolio.portfolioid=:portfolioId and o.company.companyid=:companyId and o.status<>:activeStatus";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("portfolioId", portfolioId);
        query.setParameter("activeStatus", Order.STATUS_ACCEPTED);

        return query.getResultList();
    }

}
